package com.thyme.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thyme.system.dao.Iec61850DataInstDao;
import com.thyme.system.dao.Iec61850IedInstDao;
import com.thyme.system.dao.Iec61850LdDao;
import com.thyme.system.service.Iec61850IedInstService;
import com.thyme.system.vo.Iec61850DataInstVo;
import com.thyme.system.vo.Iec61850IedInstVo;
import com.thyme.system.vo.Iec61850LdVo;
import com.thyme.system.vo.IedNameEnumVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author nana
 * @since 2021-03-17
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Iec61850IedInstServiceImpl implements Iec61850IedInstService {

    private final Iec61850IedInstDao iec61850IedInstDao;
    private final Iec61850DataInstDao iec61850DataInstDao;
    private final Iec61850LdDao iec61850LdDao;

    @Override
    public int deleteByConfigId(Integer id) {
        return iec61850IedInstDao.deleteById(id);
    }

    @Override
    public void insert(Iec61850IedInstVo iec61850IedInstVo, Set<Iec61850DataInstVo> iec61850DataInstVos, List<Iec61850LdVo> idVoList) {

        Integer id = iec61850IedInstDao.getByIEDName(iec61850IedInstVo.getIedName());
        if (id == null) {
            iec61850IedInstVo.setIedState(false);
            iec61850IedInstVo.setCreateTime(new Date());
            iec61850IedInstVo.setUpdateTime(new Date());
            iec61850IedInstDao.insert(iec61850IedInstVo);
//            for(Iec61850LdVo  tmp : idVoList){
//                tmp.setIec61850IedId(id);
//                iec61850LdDao.insert(tmp);
//            }
        } else {
            iec61850IedInstVo.setId(id);
            iec61850IedInstVo.setUpdateTime(new Date());
            iec61850IedInstDao.updateById(iec61850IedInstVo);
//            for(Iec61850LdVo  idVo : idVoList){
//                idVo.setIec61850IedId(id);
//                iec61850LdDao.updateById(idVo);
//            }
        }









        iec61850DataInstDao.deleteByIec61850IedInstId(id);
        for (Iec61850DataInstVo tmp : iec61850DataInstVos) {
            tmp.setIec61850IedInstId(iec61850IedInstVo.getId());
            iec61850DataInstDao.insert(tmp);
        }
    }


    @Override
    public Integer getByIEDName(String iedName) {
        return iec61850IedInstDao.getByIEDName(iedName);
    }

    @Override
    public IPage<Iec61850IedInstVo> getAll(Page page) {
        return iec61850IedInstDao.getAll(page);
    }

    @Override
    public Iec61850IedInstVo getById(Integer id) {
        return iec61850IedInstDao.selectById(id);
    }

    @Override
    public boolean updateConfig(Iec61850IedInstVo iec61850IedInstVo) {
        iec61850IedInstVo.setUpdateTime(new Date());
        return iec61850IedInstDao.updateById(iec61850IedInstVo)>0;
    }

    @Override
    public List<IedNameEnumVO> getLEDNameEnumList() {
        return iec61850IedInstDao.getLEDNameEnumList();
    }
}
